#include "task_manager.h"

/* 按键扫描任务 */
void ScanKeyTask(void)
{
    /* 扫描按键并获取事件（包含组合键检测） */
    FML_KEY_Scan(HAL_GetTick());
    
    /* 菜单任务处理 */
    cotMenu_Task();
}

/* LED扫描任务 */
void ScanLedTask(void)
{
    cotLed_Ctrl(HAL_GetTick());
}

/* 喂狗任务 */
void WatchdogTask(void)
{
    iwdg_feed();
}

/* 调度测试任务 */
void TestScheduleTask(void)
{
    static uint32_t counter = 0;
    static uint32_t lastTime = 0;
    uint32_t currentTime = HAL_GetTick();
    counter++;
    printf("Task executed: %ld, interval: %ldms\r\n", (long)counter, (long)currentTime - lastTime);
    lastTime = currentTime;
}

/* RTC时钟显示任务 */
void RTCDisplayTask(void)
{
    static uint8_t last_sec = 0xFF; /* 上次显示的秒数，初始化为无效值 */
    char date_str[20];              /* 日期字符串缓冲区 */
    char time_str[20];              /* 时间字符串缓冲区 */
    char week_str_buf[15];          /* 星期字符串缓冲区 */
    
    /* 获取当前RTC时间 */
    rtc_get_time();
    
    /* 只有当秒数发生变化时才更新显示，避免频繁刷屏 */
    if (calendar.sec != last_sec)
    {
        last_sec = calendar.sec;
        
        /* 星期字符串数组 */
        //const char* week_names[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
        const char* week_names[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
        
        /* 使用sprintf格式化字符串 */
        sprintf(date_str, "Date:%04d-%02d-%02d", calendar.year, calendar.month, calendar.date);
        sprintf(time_str, "Time:%02d:%02d:%02d", calendar.hour, calendar.min, calendar.sec);
        sprintf(week_str_buf, "Week:%s", week_names[calendar.week]);
        
        /* 在LCD上显示格式化后的时间信息 */
        LCD_ShowString(0, 60, (uint8_t*)date_str, WHITE, BLACK, 32, 0);
        LCD_ShowString(0, 100, (uint8_t*)time_str, WHITE, BLACK, 32, 0);
        LCD_ShowString(0, 140, (uint8_t*)week_str_buf, WHITE, BLACK, 32, 0);
        
        /* 可选：在串口输出时间信息用于调试 */
//        printf("RTC Time: %04d-%02d-%02d %02d:%02d:%02d Week:%s\r\n", 
//               calendar.year, calendar.month, calendar.date,
//               calendar.hour, calendar.min, calendar.sec,
//               week_names[calendar.week]);
    }
}
